package com.cskaoyan.aspect;

import com.cskaoyan.utils.HibernateValidatorUtils;
import jdk.nashorn.internal.ir.JoinPredecessor;
import lombok.SneakyThrows;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.validation.BindingResult;

/*
 * @Auther:王贲
 * @Date:2022/9/9
 * @Description:
 * @VERSON:1.8
 */
@Component
@Aspect
public class ValidAspect {

    @Pointcut("@annotation(com.cskaoyan.anno.ValidAnno)")
    public void pt() {
    }

    @Around("pt()")
    @SneakyThrows
    public Object aspectValid(ProceedingJoinPoint point){
        Object[] args = point.getArgs();
        BindingResult bindingResult = null;
        for (Object arg : args) {
            if (arg instanceof BindingResult) {
               bindingResult = (BindingResult) arg;
            }
        }
        HibernateValidatorUtils.valid(bindingResult);

        Object proceed = point.proceed();
        return proceed;
    }
}
